package com.yeban.javase.day12.test.streamtest;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * @Desc TODO
 * @Author yeban
 * @Date 2025/10/22
 *
 * 9.将员工按照工资由高到低排序，显示名字
 */
public class StreamTest09 {

    public static void main(String[] args) {
        List<Employee3> employees = Arrays.asList (
                new Employee3 ("张三", 7500.0),
                new Employee3 ("李四", 8200.0),
                new Employee3 ("王五", 9000.0),
                new Employee3 ("赵六", 7800.0),
                new Employee3 ("孙七", 10000.0));

        // sorted 方法    先按薪资升序排序，再用 reversed() 翻转为降序
        System.out.println("---------- sorted方法 ----------");
        employees.stream().sorted(Comparator.comparingDouble(Employee3::getSalary).reversed())
                .map(Employee3::getName)
                .forEach(System.out::println);

        // 重写自定义类中的Compare逻辑
        System.out.println("---------- 重写自定义类中的Compare逻辑 ----------");
        employees.stream().sorted().map(Employee3::getName).forEach(System.out::println);

        // 使用lambda自定义比较器
        System.out.println("---------- 使用lambda自定义比较器 ----------");
        employees.stream().sorted((e1, e2) -> e2.getSalary().compareTo(e1.getSalary()))
                .map(Employee3::getName).
                forEach(System.out::println);

        // 方法引用 需要reverse翻转
        System.out.println("---------- 方法引用 ----------");
        employees.stream().sorted(Comparator.comparing(Employee3::getSalary).reversed())
                .map(Employee3::getName)
                .forEach(System.out::println);

    }
}

class Employee3 implements Comparable<Employee3> {
    private String name;
    private Double salary;

    public Employee3() {
    }

    public Employee3(String name, Double salary) {
        this.name = name;
        this.salary = salary;
    }

    @Override
    public int compareTo(Employee3 o) {
        return o.salary.compareTo(this.salary);
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return salary
     */
    public Double getSalary() {
        return salary;
    }

    /**
     * 设置
     * @param salary
     */
    public void setSalary(Double salary) {
        this.salary = salary;
    }

    public String toString() {
        return "Employee{name = " + name + ", salary = " + salary + "}";
    }
}
